package com.edu.test;

import com.edu.bean.File;
import com.edu.bean.User;
import com.edu.mapper.FileMapper;
import com.edu.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Mytest {
    private SqlSession session ;


    @Before
    public void init() throws IOException {
        String path = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(path);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        session = sessionFactory.openSession();
    }
    @Test
    public  void  test01(){
        FileMapper fileMapper = session.getMapper(FileMapper.class);
        File file = fileMapper.selectFileById(2);
        System.out.println(file);
    }
    @Test
    public void test02(){
        FileMapper fileMapper = session.getMapper(FileMapper.class);
        File file = new File();
        file.setFileName("001");
        file.setFileSize(1024);
        file.setCreateTime(new Date());
        file.setTypeId(1);
        file.setParentFolderId(1);
        file.setCreatorId(1);
        int i = fileMapper.insertFile(file);
        System.out.println(i);
        session.commit();
        session.close();
    }
    @Test
    public  void  test03(){
        FileMapper fileMapper = session.getMapper(FileMapper.class);
        int i = fileMapper.deleteFileById(31);
        System.out.println(i);
        session.commit();
        session.close();
    }
    @Test
    public  void  test04(){
        FileMapper fileMapper = session.getMapper(FileMapper.class);
        PageHelper.startPage(1,10);
        List<File> files = fileMapper.selectAll();
        for (File file : files) {
            System.out.println(file);
        }
    }
    @Test
    public  void  test05(){
        UserMapper userMapper = session.getMapper(UserMapper.class);
        User user = userMapper.selectUserByName("张三");
        System.out.println(user);
        System.out.println(user.getFiles());
    }
    @Test
    public  void  test06(){
        UserMapper userMapper = session.getMapper(UserMapper.class);
        List<User> users = userMapper.selectAllUser();
        for (User user : users) {
            System.out.println(user);
            System.out.println(user.getFiles());
        }
    }

    @Test
    public  void  test07(){
        FileMapper mapper = session.getMapper(FileMapper.class);
        List<File> files = mapper.selectAll2();
        for (File file : files) {
            System.out.println(file);
            System.out.println(file.getFileType());
        }

    }


    @Test
    public  void  test08(){
        UserMapper userMapper = session.getMapper(UserMapper.class);
        User user = userMapper.selectUserById(1);
        System.out.println(user);
        System.out.println(user.getFiles());
    }
}
